//
//  NewFeatureController.swift
//  WSWB
//
//  Created by 刘 伟生 on 16/1/11.
//  Copyright © 2016年 seasa. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "NewFeatureController"


class NewFeatureController: UICollectionViewController {

    // MARK:- 属性
    @IBOutlet weak var FlowLayout: UICollectionViewFlowLayout!
    
    /// 新特性总页数
    private var maxPageCount = 4
    

    //MARK:- 系统回调方法
    override func viewDidLoad() {
        super.viewDidLoad()

        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        
        //设置布局
        FlowLayout.itemSize = UIScreen.mainScreen().bounds.size
        FlowLayout.scrollDirection = .Horizontal
        FlowLayout.minimumInteritemSpacing = 0
        FlowLayout.minimumLineSpacing = 0
        
        //设置collectionView
        
        self.collectionView?.bounces = false
        self.collectionView?.pagingEnabled = true
        self.collectionView?.showsHorizontalScrollIndicator = false
        self.collectionView?.showsVerticalScrollIndicator = false
        
        
        collectionView?.registerClass(WSNetFeatureCell.classForCoder(), forCellWithReuseIdentifier: "NewFeatureController")
        
      
        
    }
    
    
    //MARK:- UICollectionView数据源方法
    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return maxPageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView .dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! WSNetFeatureCell

        cell.index = indexPath.row
        if indexPath.row == maxPageCount - 1 {
//            cell.startBtn.hidden = false
//            cell.startAnimation()
        }
        
        return cell
    }
    
    //这个方法是显示完一个Cell才会调用
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        //首选获取最后一个Cell的索引
        guard let path = collectionView.indexPathsForVisibleItems().last  else {
            return
        }
        
        //获取最后一个cell
        guard let cell = collectionView.cellForItemAtIndexPath(path) as? WSNetFeatureCell else {
            return
        }
        
        if path.row == maxPageCount - 1 {
            cell.startBtn.hidden = false
            cell.startAnimation()
        }
    }
    

 
}


//创建一个cell的类
class WSNetFeatureCell : UICollectionViewCell {
    
    //MARK:- cell的属性
    
    //懒加载image和button
    private lazy var iconImage = UIImageView()
    private lazy var startBtn : UIButton = {
        let btn = UIButton()
        btn.setBackgroundImage(UIImage(named: "new_feature_button"), forState: .Normal)
        btn.setBackgroundImage(UIImage(named: "new_feature_button_highlighted"), forState: .Highlighted)
        btn.hidden = true
        btn.addTarget(self, action: "startWB", forControlEvents: .TouchUpInside)
        return btn
    }()
    
    //cell的索引属性
    var index : Int = 0
        {
        didSet{//相当于重写set方法
            iconImage.image = UIImage(named: "new_feature_\(index + 1)")
            //设置按钮为隐藏
            startBtn.hidden = true
        }
    }
    
    
    //系统的构造方法
    //为了实现方法既可以是从xib加载,也可以从代码实现所以需要都需要实现
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        set()
        
    }

    required init?(coder aDecoder: NSCoder) {
        super.init(coder: aDecoder)
        set()
    }
    
    //collectionViewCell上子控件
    func set(){
        addSubview(iconImage)
        addSubview(startBtn)
        
        //布局子控件
        iconImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(0)
        }
        
        startBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(centerXAnchor)
            make.bottom.equalTo(contentView.bottomAnchor).offset(-160)
        }
    }
    
    //只有在Cell完全显示的时候才动用动画,那么就需要在collectionView中的代理方法才可以实现
    
    
    //当显示按钮的时候,使按钮出现动画
    func startAnimation() {
        //先设置按钮形状为0
        startBtn.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1.0, delay: 0.1, usingSpringWithDamping: 0.9, initialSpringVelocity: 20, options: .LayoutSubviews, animations: { () -> Void in
            self.startBtn.transform = CGAffineTransformIdentity
            }) { (isFinish) -> Void in
//                print("结束动画")
        }
       
    }
    
    //mark:- 时间监控
    @objc private func startWB() {
        print("niya")
        //跳转到主页
        let storyBoard = UIStoryboard(name: "Main", bundle: nil)
        let TabVC = storyBoard.instantiateInitialViewController()
        
        let window = UIApplication.sharedApplication().keyWindow
        
        window?.rootViewController = TabVC
        
       
    }
    
}

